//
//  AFNData.m
//  Docter
//
//  Created by liuzhishan on 15/12/14.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "AFNData.h"
#import "AFNetworking.h"
@implementation AFNData

static UIActivityIndicatorView   *activityIndicator = nil;

+(void)starActivity
{
    if (activityIndicator == nil) {
        
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            
            activityIndicator = [[UIActivityIndicatorView alloc] init];
            
        });
    }
    
    UIWindow *windows =   [UIApplication sharedApplication].keyWindow;
    
    
    activityIndicator.center = windows.center;
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    [windows addSubview:activityIndicator];
    
    [windows bringSubviewToFront:activityIndicator];
    
    [activityIndicator startAnimating];
    
    
    UIApplication *application = [UIApplication sharedApplication];
    application.networkActivityIndicatorVisible = YES;
}

+(void)finishActivity
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [activityIndicator stopAnimating];

}

+(void)alertMyNetWork
{
    [AlertNetWork AlertViewMessage:@"网络不好,请检查网络" andDismissTime:1.0];
}


+(void)alertMySever
{
    [AlertNetWork AlertViewMessage:@"服务器抽风，程序猿GG正在抢救" andDismissTime:1.0];
}

+(void)getAFNDataWithUrl:(NSString *)str  success:(void (^)(id responseObject))success
                 failure:(void (^)(NSError *error))failure
{
    [AFNData starActivity];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];

    
    
    NSString *url_string = str;
    
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setTimeoutInterval:MYAFNTIMEOUT];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSLog(@"%@",responseObject);
        
        NSDictionary *dic = responseObject;
        
        if ([dic[@"code"]integerValue] == 0) {
            
             success(responseObject);
        }
        
        else
        {
            [self alertMySever];
        }
       
        
        [AFNData finishActivity];
        
        
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
   
        [AFNData finishActivity];
   
        
        failure(error);
        [AFNData alertMyNetWork];
        
    }];

    
}

+(void)postAFNDataWithUrl:(NSString *)str  success:(void (^)(id responseObject))success
                 failure:(void (^)(NSError *error))failure
{
    
    
    [AFNData starActivity];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = str;
    
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setTimeoutInterval:MYAFNTIMEOUT];
    
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    
    [manager POST:url_string parameters:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
        
        [netWorkManager stopMonitoring];
        
        success(responseObject);
        
        [AFNData finishActivity];
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        
         failure(error);
        
        [AFNData finishActivity];
        
        [AFNData alertMyNetWork];
        
    }];
    
     
}


+(void)postBodyAFNDataWithUrl:(NSString *)str andBodyDic:(NSDictionary *)parameter success:(void (^)(id responseObject))success
                      failure:(void (^)(NSError *error))failure
{
    
    [AFNData starActivity];
   
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = str;
    
//    NSURL *url = [NSURL URLWithString:url_string];
//    
//    
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    [request setHTTPMethod:@"POST"];
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setTimeoutInterval:MYAFNTIMEOUT];
    
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    
    [manager POST:url_string parameters:parameter success:^(NSURLSessionDataTask *operation, id responseObject) {
        
        [netWorkManager stopMonitoring];
        
        success(responseObject);
        
        [AFNData finishActivity];
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        
        failure(error);
        
        [AFNData finishActivity];
        [AFNData alertMyNetWork];
        
    }];
    
    
}



@end
